package com.zueb.web.servlet;

import com.zueb.pojo.Vaccination;
import com.zueb.service.VaccinationService;
import com.zueb.service.impl.VaccinationServiceImpl;
import com.zueb.util.ResponseWriterUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "VaccinationServlet", value = "/VaccinationServlet")
public class VaccinationServlet extends HttpServlet {

    VaccinationService vaccinationService = new VaccinationServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转发请求到doPost
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //转码
        request.setCharacterEncoding("UTF-8");
        //action表示用来区分是什么操作
        String action = request.getParameter("action");

        if (action.equals("addVaccination")) {
            addVaccination(request, response);
        } else if (action.equals("toShowVaccination")) {
            toShowVaccination(request, response);
        } else if (action.equals("showAllVaccinationByPage")) {
            showAllVaccinationByPage(request, response);
        }
    }

    private void showAllVaccinationByPage(HttpServletRequest request, HttpServletResponse response) {
        Integer curPage = Integer.parseInt(request.getParameter("curPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Integer facultyId = Integer.parseInt(request.getParameter("facultyId"));
        Integer classId = Integer.parseInt(request.getParameter("classId"));

        List<Vaccination> vaccinations = vaccinationService.showAllVaccinationByPage(curPage, pageSize, facultyId, classId);
        int count = vaccinationService.showAllVaccinationCount(facultyId, classId);

        Map<String, Object> map = new HashMap<>();
        map.put("vaccinations", vaccinations);
        map.put("count", count);

        ResponseWriterUtils.writer(map, response);


    }

    /**
     * 查看个人的疫苗接种情况
     *
     * @param request
     * @param response
     */
    private void toShowVaccination(HttpServletRequest request, HttpServletResponse response) {
        //获取前端传来的参数
        String identification = request.getParameter("identification");
        //调用方法查询疫苗接种情况集合
        List<Vaccination> vaccinations = vaccinationService.showVaccination(identification);
        //将参数封装入Map传入前端
        Map<String, Object> map = new HashMap<>();
        map.put("vaccinations", vaccinations);
        //返回前端Map集合
        ResponseWriterUtils.writer(map, response);
    }

    /**
     * 添加疫苗接种记录
     *
     * @param request
     * @param response
     */
    private void addVaccination(HttpServletRequest request, HttpServletResponse response) {
        String userNumber = request.getParameter("userNumber");
        String name = request.getParameter("name");
        String identification = request.getParameter("identification");
        String vacName = request.getParameter("vacName");
        String factory = request.getParameter("factory");
        String datetime = request.getParameter("date");
        int frequency = Integer.parseInt(request.getParameter("frequency"));

        Vaccination vaccination = new Vaccination();
        vaccination.setUsername(name);
        vaccination.setIdentification(identification);
        vaccination.setVaccineName(vacName);
        vaccination.setFactory(factory);
        vaccination.setFrequency(frequency);
        vaccination.setTime(datetime);

        int flag = vaccinationService.addVaccination(vaccination);

        ResponseWriterUtils.writerFlag(flag, response);
    }
}
